﻿using UnityEngine;
using System.Collections;

public class PauseButton : MonoBehaviour
{
    private bool isOver;
    private TextMesh text;

    void Start()
    {
        text = gameObject.GetComponent<TextMesh>();
    }

    void Update()
    {
        Touch[] touch = Input.touches;

        if(Input.touchCount > 0)
        {
            Ray ray = Camera.main.ScreenPointToRay(new Vector3(touch[0].position.x, touch[0].position.y, 0));
            RaycastHit hit;

            if(Physics.Raycast(ray, out hit, Mathf.Infinity))
            {
                if(hit.collider.gameObject == gameObject)
                {
                    if(touch[0].phase == TouchPhase.Began)
                    {
                        this.isOver = true;
                    }

                    if(touch[0].phase == TouchPhase.Ended)
                    {
                        if(gameObject.tag == "PauseResume")
                        {
                            isOver = false;
                            transform.parent.gameObject.SetActive(false);
                            Time.timeScale = 1 - Time.timeScale;
                        }

                        if(gameObject.tag == "PauseMenu")
                        {
                            Application.LoadLevel("Menu");
                        }
                    }
                }
                else
                {
                    this.isOver = false;
                }
            }
        }

        if(isOver)
        {
            text.color = new Color32(16, 16, 175, 255);
        }
        else
        {
            text.color = Color.white;
        }
    }
}
